package fr.imag.ufrima.ima.project.ui.tools;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import fr.imag.ufrima.ima.project.core.operations.Contours;
import fr.imag.ufrima.ima.project.core.operations.GrayScale;
import fr.imag.ufrima.ima.project.ui.main.ImageProject;

@SuppressWarnings("serial")
public class Outils extends JPanel {
	private Logger logger = Logger.getLogger(getClass().getName());

	private ImageProject imageProject;
	private JButton btnNiveauxGris;
	private JButton btnFusion;
	private JButton btnAccentuation;

	/**
	 * Create the panel.
	 */
	public Outils(ImageProject imageProject) {
		this.imageProject = imageProject;

		setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 180 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0 };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JLabel lblSelectionnerLoutil = new JLabel("Selectionner l'outil:");
		GridBagConstraints gbc_lblSelectionnerLoutil = new GridBagConstraints();
		gbc_lblSelectionnerLoutil.insets = new Insets(10, 0, 5, 0);
		gbc_lblSelectionnerLoutil.gridx = 0;
		gbc_lblSelectionnerLoutil.gridy = 0;
		panel.add(lblSelectionnerLoutil, gbc_lblSelectionnerLoutil);

		// Boutton Découper
		JButton btnDecouper = new JButton("Découper");
		GridBagConstraints gbc_btnDecouper = new GridBagConstraints();
		gbc_btnDecouper.insets = new Insets(0, 0, 5, 0);
		gbc_btnDecouper.fill = GridBagConstraints.BOTH;
		gbc_btnDecouper.gridx = 0;
		gbc_btnDecouper.gridy = 1;
		btnDecouper.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Outils.this.imageProject.getSelectorListener().setActive();
			}
		});
		panel.add(btnDecouper, gbc_btnDecouper);

		// Boutton Découper
		JButton btnHistogram = new JButton("Histogramme");
		GridBagConstraints gbc_btnHistogram = new GridBagConstraints();
		gbc_btnHistogram.insets = new Insets(0, 0, 5, 0);
		gbc_btnHistogram.fill = GridBagConstraints.BOTH;
		gbc_btnHistogram.gridx = 0;
		gbc_btnHistogram.gridy = 2;
		btnHistogram.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Outils.this.imageProject.getToolsPanel().getToolWindowHistogram().setActive(true);
			}
		});
		panel.add(btnHistogram, gbc_btnHistogram);

		// Boutton niveaux de gris
		btnNiveauxGris = new JButton("Niveaux Gris");
		btnNiveauxGris.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Outils.this.applyGrayScale();
			}
		});
		GridBagConstraints gbc_btnNiveauxGris = new GridBagConstraints();
		gbc_btnNiveauxGris.insets = new Insets(0, 0, 5, 0);
		gbc_btnNiveauxGris.fill = GridBagConstraints.BOTH;
		gbc_btnNiveauxGris.gridx = 0;
		gbc_btnNiveauxGris.gridy = 3;
		panel.add(btnNiveauxGris, gbc_btnNiveauxGris);

		// Boutton Flou
		JButton btnFlou = new JButton("Flou");
		GridBagConstraints gbc_btnFlou = new GridBagConstraints();
		gbc_btnFlou.insets = new Insets(0, 0, 5, 0);
		gbc_btnFlou.fill = GridBagConstraints.BOTH;
		gbc_btnFlou.gridx = 0;
		gbc_btnFlou.gridy = 4;
		btnFlou.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Outils.this.imageProject.getToolsPanel().getToolWindowFlou().setActive(true);
			}
		});
		panel.add(btnFlou, gbc_btnFlou);

		// Boutton fusion
		btnFusion = new JButton("Fusion");
		btnFusion.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Outils.this.imageProject.getToolsPanel().getToolWindowFusion().setActive(true);
			}
		});
		GridBagConstraints gbc_btnFusion = new GridBagConstraints();
		gbc_btnFusion.insets = new Insets(0, 0, 5, 0);
		gbc_btnFusion.fill = GridBagConstraints.BOTH;
		gbc_btnFusion.gridx = 0;
		gbc_btnFusion.gridy = 5;
		panel.add(btnFusion, gbc_btnFusion);

		// Boutton Redimensionnement
		JButton btnRedimensionner = new JButton("Redimensionner");
		GridBagConstraints gbc_btnRedimensionner = new GridBagConstraints();
		gbc_btnRedimensionner.fill = GridBagConstraints.BOTH;
		gbc_btnRedimensionner.insets = new Insets(0, 0, 5, 0);
		gbc_btnRedimensionner.gridx = 0;
		gbc_btnRedimensionner.gridy = 6;
		btnRedimensionner.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Outils.this.imageProject.getToolsPanel().getToolWindowResize().setActive(true);
			}
		});
		panel.add(btnRedimensionner, gbc_btnRedimensionner);

		JButton btnFiltres = new JButton("Filtres");
		btnFiltres.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Outils.this.imageProject.getToolsPanel().getToolWindowFilter().setActive(true);
			}
		});
		GridBagConstraints gbc_btnFiltres = new GridBagConstraints();
		gbc_btnFiltres.fill = GridBagConstraints.BOTH;
		gbc_btnFiltres.insets = new Insets(0, 0, 5, 0);
		gbc_btnFiltres.gridx = 0;
		gbc_btnFiltres.gridy = 7;
		panel.add(btnFiltres, gbc_btnFiltres);

		// Boutton Accentuation
		btnAccentuation = new JButton("Accentuation");
		btnAccentuation.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				Outils.this.imageProject.getToolsPanel().getToolWindowSharpen().setActive(true);
								
			}
		});
		GridBagConstraints gbc_btnAccentuation = new GridBagConstraints();
		gbc_btnAccentuation.insets = new Insets(0, 0, 5, 0);
		gbc_btnAccentuation.fill = GridBagConstraints.BOTH;
		gbc_btnAccentuation.gridx = 0;
		gbc_btnAccentuation.gridy = 8;
		panel.add(btnAccentuation, gbc_btnAccentuation);
		
		JButton btnContours = new JButton("Contours");
		btnContours.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Outils.this.applyContours();
			}
		});
		GridBagConstraints gbc_btnContours = new GridBagConstraints();
		gbc_btnContours.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnContours.insets = new Insets(0, 0, 5, 0);
		gbc_btnContours.gridx = 0;
		gbc_btnContours.gridy = 9;
		panel.add(btnContours, gbc_btnContours);

	}

	private void applyGrayScale() {
		GrayScale grayScale = new GrayScale(imageProject.getCurrentImageDocument().getImage());
		
		imageProject.applyOperation(grayScale);			
	}
	
	private void applyContours() {
		Contours sharpen = new Contours(imageProject.getCurrentImageDocument().getImage());				
		
		imageProject.applyOperation(sharpen);
	}
	

}
